﻿using Adp;
using Adp.Common;
using Adp.Core.Services;
using Adp.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json.Linq;
using Yqd.Enm;
using Yqd.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Yqd.Core;

namespace Yqd.Controllers.Filter
{

    public class MasterAttribute : ActionFilterAttribute
    {
        private 角色类型[] JobLevel;

        public MasterAttribute(params 角色类型[] JobLevel)
        {
            this.JobLevel = JobLevel;
        }

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var wx = WxUser.LogonInfo; 
            if (JobLevel.Length == 0 || JobLevel.Any(it=>wx.Role.Contains(it)))
            {
                return;
            }
            context.Result = new JsonResult(new { success = false, JobLevel = 0, msg = "权限不足!" });
        }
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            base.OnActionExecuted(context);

            var wx = WxUser.LogonInfo;

            var jr = (JsonResult)context.Result;
            if (jr != null)
            {
                var r = JObject.FromObject(jr.Value);
                r["Master"] = JObject.FromObject(new
                {
                    wx.Role,
                    wx.Name,
                    wx.UnitCode,
                    wx.EmployeeCode
                }); 
                context.Result = new JsonResult(r);
            }
        }

    }

}
